<?php


namespace app\admin\utils;


use app\model\ModelTask;
use app\model\ModelUser;
use app\model\ModelUserTask;

class Utils
{
    //生成邀请码
    public static function getInvitationCode($length = 6) :string{
        static $sourceString = [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x',
            'y', 'z'
        ];

        $code = '';
        for($i = 0; $i < $length; ++$i) {
            $code .= $sourceString[rand(0,count($sourceString) - 1)];
        }
        if(substr($code, 0,1) == '0') {
            substr($code, 1, 5).$sourceString[rand(10,count($sourceString) - 1)];
        }
        //验证邀请码唯一性
        $userModel = new ModelUser();
        $res = $userModel->where('my_invitation_code', '=',$code)->find();
        if($res) {
            $code = self::getInvitationCode();
        }
        return strtoupper($code);
    }


}